<?php
class MeetingAction extends Action{
	
/**
 * 获取个人会议列表 [no paging]
 * @param  cid uid 
 * create by zhanghao in 2014/11/29 11:22
 */
	public function get_meeting_calendar_list(){
		
		if(!Database($_REQUEST["cid"]))exit;
		$_REQUEST['uid']=check_null(40002,true,$_REQUEST['uid']);
		
		$rs=D('Meeting')->get_meeting_calendar_list($_REQUEST);
		if($rs){
			return_json('0',$rs);
		}else{
			return_json('40001');
		}
	}
/**
  *select meeting list
  *update author: zhanghao
  *update time  : 2014-11-13 09:08
  ********************************************/
	public function get_meeting_list(){
		$_REQUEST['count']=check_null(10,false,$_REQUEST['count']);
		$_REQUEST['page'] =check_null(1 ,false,$_REQUEST['page']);
		$_REQUEST['uid'] =check_null(40002,true,$_REQUEST['uid']);
		if(!Database($_REQUEST["cid"]))exit;
		$rs=D('Meeting')->get_meeting_list($_REQUEST);
		if($rs){
			return_json('0',$rs);
		}else{
			return_json('40001');
		}	
	}
/**
  *select meeting rooom list
  *update author: zhanghao
  *update time  : 2014-11-12 15:20
  ********************************************/
    public function check_idle_meeting_room(){
        if(!Database($_REQUEST["cid"]))exit;
        $rs=D('Meeting')->check_idle_meeting_room($_REQUEST);
        if($rs){
            return_json(0,$rs);
        }else{
            return_json(-1);
        }
    }
/**
  *create meeting
  *update author: zhanghao
  *update time  : 2014-11-18 11:12
  ********************************************/	
	public function create_meeting(){
        if(!Database($_POST['cid']))exit;
		$_POST['uid']				=check_null(40002,true,$_POST['uid']);
		$_POST['meeting_title'] 	=check_null(40057,true,$_POST['meeting_title']);
		$_POST['meeting_startTime'] =check_null(40058,true,$_POST['meeting_startTime']);
		$_POST['meeting_endTime'] 	=check_null(40059,true,$_POST['meeting_endTime']);
		if($_POST['meeting_endTime']<time())return 40059;
		$_POST['meeting_local'] 	=check_null(40060,true,$_POST['meeting_local']);
		$_POST['meeting_join'] 		=check_null(40061,true,$_POST['meeting_join']);
		$_POST['schedule_date']		=date('Y-m-d',$_POST['meeting_startTime']);
		$_POST['meeting_local_type']=$_POST['meeting_local_type']==NULL?0:$_POST['meeting_local_type'];
		$_POST['time_insert']		=time();

		//选择会议室查看会议室预定情况
		if($_POST['meeting_local_type'] != 1){

			$_POST['meeting_room_id']	=check_null(40076,true,$_POST['meeting_room_id']);
			//init meeting_local
			$_POST['meeting_local']		=D('Meeting')->get_meeting_local($_POST['meeting_room_id']);
			//预定会议室
//			$redis = new Redis();//从Redis服务器拿到redis实例
//
//			$data = '{"ID:i"}';
//			$redis->connect('Redis服务器IP', 6379);
//			// while (true) {
//				$redis->lPush('list1', "data");
//			// }
//			$res = $redis->lPop("list1");
//			$i = 0;

			if(!(D('Meeting')->schedule_meeting_room($_POST)))return_json(40077);
		}

		$rs=D('Meeting')->create_meeting($_POST);
		if($rs){
			return_json(0);
		}else{
			return_json(-1);
		}
	}

/**
  *get meeting details
  *update author: zhanghao
  *update time  : 2014-11-18 11:12
  ********************************************/
	public function get_meeting_details(){
		$_REQUEST["meeting_id"] = check_null(40062,true,$_REQUEST["meeting_id"]);
		if(!Database($_REQUEST["cid"]))exit;
		$_POST['time_insert']=time();
		$rs=D('Meeting')->get_meeting_by_id($_REQUEST['meeting_id']);
		if($rs){
			return_json(0,$rs);
		}else{
			return_json(-1);
		}
	}
	
	//查询参会人信息
	public function get_meeting_join(){
		switch(''){
			case $_REQUEST['meeting_id']:return_json(40062);return;
		}
		if(!Database($_REQUEST["cid"]))exit;
		$rs=D('Meeting')->get_meeting_join($_REQUEST['meeting_id']);
		if($rs){
			return_json(0,$rs);
		}else{
			return_json(40001);
		}
	}
	
	//修改会议状态
	public function update_join_state(){
		if(!Database($_REQUEST["cid"]))exit;
		switch(''){
			case $_REQUEST['uid']:return_json(40002);return;
			case $_REQUEST['meeting_id']:return_json(40062);return;
			case $_REQUEST['join_state']:return_json(40063);return;
		}
		$rs=D('Meeting')->update_join_state($_REQUEST);
		if($rs){
			return_json(0);
		}else{
			return_json(-1);
		}
	}
/*
 * @name
 * 修改会议信息
 *
 * @arg
 * cid int 缺省 返回 40038
 * uid int 缺省 返回 40002
 * meeting_title 缺省 返回 40057
 * meeting_startTime 缺省 返回 40058
 * meeting_endTime 缺省 返回 40059
 * meeting_local 会议室名字
 * meeting_description 可选
 * meeting_join 参会人id字符串 // meeting_join 格式为 1,2,3,4,5
 * meeting_room_id 会议室id
 *
 * @update
 * 2014/12/10 10:26 zhanghao
 * */
	public function update_meeting_info(){
		if(!Database($_REQUEST["cid"]))exit;
		switch(''){
			case $_REQUEST['uid']:return_json(40002);return;
			case $_REQUEST['meeting_id']:return_json(40062);return;
		}
		$_REQUEST['time_update']=time();
		$rs=D('Meeting')->update_meeting_info($_REQUEST);
		if($rs){
			return_json(0);
		}else{
			return_json(-1);
		}	
	}
	
	//获取一条默认信息
	public function get_meeting_default(){
		if(!Database($_REQUEST["cid"]))exit;
		switch(''){
			case $_REQUEST["uid"]:return_json(40002);return;
			case $_REQUEST['meeting_startTime']:return_json(40058);return;
			case $_REQUEST['meeting_endTime']:return_json(40059);return;
		}
		$rs=D('Meeting')->check_idle_meeting_room_by_uid($_REQUEST);
		if($rs){
			return_json(0,$rs);
		}else{
			return_json(-1);
		}
	}
	
	//获取所有的楼座信息
	public function get_meeting_room_building(){
		if(!Database($_REQUEST["cid"]))exit;
		switch(''){
			case $_REQUEST['local_id']:return_json(40069);return;
		}
		$rs=D('Meeting')->get_meeting_room_building($_REQUEST['local_id']);
		if($rs){
			return_json(0,$rs);
		}else{
			return_json(40001);
		}
	}
	
	//获取所有的楼层信息
	public function get_meeting_room_floor(){
		if(!Database($_REQUEST["cid"]))exit;
		switch(''){
			case $_REQUEST['building_id']:return_json(40070);return;
		}
		$rs=D('Meeting')->get_meeting_room_floor($_REQUEST['building_id']);
		if($rs){
			return_json(0,$rs);
		}else{
			return_json(-1);
		}
	}
	
	//搜索会议返回会议列表
	public function search_meeting(){
		if(!Database($_REQUEST["cid"]))exit;
		switch(''){
			case $_REQUEST['conditions']:return_json(40039);return;
			case $_REQUEST['searchType']:return_json(40072);return;
			case $_REQUEST["count"]:$_REQUEST['count']=10;
			case $_REQUEST['page']:$_REQUEST['page']=1;
		}
		$rs=D('Meeting')->search_meeting($_REQUEST);
		if($rs){
			return_json(0,$rs);
		}else{
			return_json('40001');
		}	
	}
	
	//取消会议接口
	public function cancel_meeting(){
		if(!Database($_POST["cid"]))exit;
		if($_POST['meeting_id']==null){return_json(40062);return;}
		$rs=D('Meeting')->cancel_meeting($_POST['meeting_id']);
		if($rs){
			return_json(0);
		}else{
			return_json(40074);
		}	
	}
	
	//获取会议室所有地区
	public function get_meeting_room_locals(){
		if(!Database($_REQUEST["cid"]))exit;
		
		$rs['local']	=D('Meeting')->get_all_local();
		$rs['building']	=D('Meeting')->get_all_building();
		$rs['floor']	=D('Meeting')->get_all_floor();
		
		$rs['building']	=!$rs['building']?NULL:$rs['building'];
		$rs['floor']	=!$rs['floor']?NULL:$rs['floor'];
		
		if($rs['local']){
			return_json(0,$rs);
		}else{
			return_json(40001);
		}
	}
	
	/*public function update_meeting_date(){
		if(!Database(100))exit;
		
		$rs=M('meeting','','DB_MEETING')->select();
		dump($rs);
		foreach($rs as $val){
			$data['meeting_date']=date('Y-m-d',$val['meeting_startTime']);
			
			$re=M('meeting','','DB_MEETING')->where('meeting_id='.$val['meeting_id'])->save($data);	
			if(!$re)
				dump('error'.$val['meeting_id']);
		}
	}*/
}